package com.jshx.sz.gdjt.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.bean.TaskListBean;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;

/**
 * 欢迎界面
 * @author caxiang
 * @time 2013-08-20
 */
public class PopActivity extends SuperActivity 
{
	
	//是否是展示图片
	private boolean isImage = false;
	
	//确定按钮
	private ImageButton sureButton;
	
	//消失按钮
	private ImageButton dismissButton;
	
	/**
	 * 列表组件
	 */
	private ListView popList;
	
	//弹出的显示图片
	private ImageView popImage;
	
	
	//列头
	private TextView popTitle;
	
	//list列表数据
	private ArrayList data=new ArrayList();
	
	//组件实例化工具
	private LayoutInflater layoutInflater;
	
	//
	private boolean isNeedShowCheckBox = false;
	
	//
	private boolean checkBoxChecked = false;
	
	/**
	 * 页面句柄
	 */
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) 
		{
			int what = msg.what;
			super.handleMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pop_activity);
		TAG = "PopActivity";
		layoutInflater = LayoutInflater.from(this);
		isImage = getIntent().getBooleanExtra("isImage", false);
		isNeedShowCheckBox = getIntent().getBooleanExtra("isNeedShowCheckBox", false);
		checkBoxChecked = getIntent().getBooleanExtra("checkBoxChecked", false);
		findView();
		initView();
		
		popList.setAdapter(myAdapter);
	}

	/**
	 * 组件实例化
	 */
	private void findView() {
		sureButton = (ImageButton)findViewById(R.id.pop_sure);
		dismissButton = (ImageButton)findViewById(R.id.pop_dismiss);
		popList = (ListView)findViewById(R.id.pop_list);
		popImage = (ImageView)findViewById(R.id.pop_image);
		popTitle = (TextView)findViewById(R.id.pop_title);
	}

	TaskListBean lb;
	
	/**
	 * 组件数据初始化
	 */
	private void initView() {
		final Intent it=getIntent();
		String operatonCode=it.getStringExtra("operatonCode");
		popTitle.setText("作业令:  "+operatonCode);
		if(isImage)
		{
			popImage.setVisibility(View.VISIBLE);
			sureButton.setVisibility(View.GONE);
			popList.setVisibility(View.GONE);
			if(ApplicationGlobal.globalDrawable != null)
			{
				popImage.setBackgroundDrawable(ApplicationGlobal.globalDrawable);
			}
		}
		else
		{
			lb = it.getParcelableExtra("taskList");
			data = lb.getreturnList();
			popList.setVisibility(View.VISIBLE);
			popImage.setVisibility(View.GONE);
			if(!isNeedShowCheckBox)
			{
				sureButton.setVisibility(View.GONE);
			}
		}
		dismissButton.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				PopActivity.this.finish();
			}
		});
		sureButton.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PopActivity.this, GetTaskListActivity.class);
				intent.putExtra("taskList", lb);
				intent.putExtra("position", it.getIntExtra("position", -1));
				setResult(RESULT_OK, intent);
				PopActivity.this.finish();
			}
		});
	}
	
	@Override
	public void finish()
	{
		super.finish();
		overridePendingTransition(R.anim.popup_enter, R.anim.popup_exit);
	}
	
	/**
	 * list数据适配器
	 */
	private BaseAdapter myAdapter = new BaseAdapter()
	{
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			convertView = layoutInflater.inflate(R.layout.poplist_item, null);
			final CheckBox check = (CheckBox)convertView.findViewById(R.id.pop_listitem_check);
			TextView orderNo = (TextView)convertView.findViewById(R.id.pop_listitem_orderNo);
			TextView assetCode = (TextView)convertView.findViewById(R.id.pop_listitem_assetCode);
			TextView assetName = (TextView)convertView.findViewById(R.id.pop_listitem_assetName);
			TextView dataNumber = (TextView)convertView.findViewById(R.id.pop_listitem_dataNumber);
			
			dataNumber.setVisibility(View.GONE);
			
			HashMap map=(HashMap)data.get(position);
			orderNo.setText("工单号:" + map.get("operationCode")+"");
			assetCode.setText("设备号:" + map.get("status")+"");
			assetName.setText("设备名称:" + map.get("stopName")+"");
			dataNumber.setText("日期" + map.get("dataNumber")+"");
			if(isNeedShowCheckBox)
			{
				check.setVisibility(View.VISIBLE);
				if(checkBoxChecked)
				{
					check.setChecked(true);
					((HashMap)data.get(position)).put("isSelect", true);
				}
				check.setOnCheckedChangeListener(new OnCheckedChangeListener() 
				{
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						((HashMap)data.get(position)).put("isSelect", isChecked);
					}
				});
				convertView.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) {
						check.setChecked(!check.isChecked());
					}
				});
			}
			else
			{
				convertView.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) {
						TextView orderNo = (TextView)v.findViewById(R.id.pop_listitem_orderNo);
						TextView assetCode = (TextView)v.findViewById(R.id.pop_listitem_assetCode);
						TextView dataNumber = (TextView)v.findViewById(R.id.pop_listitem_dataNumber);
						
					//	orderNo.setText(orderNo.getText()+">"+dataNumber.getText());
						
						Intent intent = new Intent();
						HashMap map=(HashMap)data.get(position);
						String dataNum = (String)map.get("dataNumber");
						String orderNum = (String)map.get("operationCode");
						String teamId = (String)map.get("teamId");
						String assetCode_=(String)map.get("assetCode");
						if(dataNum.equals("1"))
						{
							intent = new Intent(PopActivity.this, TaskCheckActivity.class);
						}
						else if(dataNum.equals("4"))
						{
							intent = new Intent(PopActivity.this, TaskActivityGroup.class);
						}
						intent.putExtra("orderNo", orderNum);
						intent.putExtra("assetCode", assetCode_);
						intent.putExtra("type", getIntent().getStringExtra("type"));
						intent.putExtra("teamId", teamId);
						startActivity(intent);
						PopActivity.this.finish();
					}
				});
				check.setVisibility(View.GONE);
			}
			
			
			return convertView;
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public Object getItem(int arg0) {
			if(data == null || arg0 >= data.size())
			{
				return null;
			}
			return data.get(arg0);
		}
		
		@Override
		public int getCount() {
			if(data == null)
			{
				return 0;
			}
			return data.size();
		}
	};
}
